﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SystemAnalizer.Common;
using System.IO;
using System.Xml.Serialization;
using System.Windows;

namespace SystemAnalizer.Utils
{
    public class ProgramSettings
    {
        private SettingsStruct settings;
        private string settings_file = ".\\Resources\\settings.xml";

        public ProgramSettings()
        {
            LoadSettings();
        }

        public SettingsStruct GetSettingStruct
        {
            get { return this.settings; }
        }

        private void LoadSettings()
        { 
            if (File.Exists(settings_file))
            {
                XmlSerializer SettingsXml = new XmlSerializer(typeof(SettingsStruct));
                try
                {
                    Stream str = new FileStream(settings_file, FileMode.Open);
                    try
                    {
                        SettingsStruct LoadSetting = (SettingsStruct)SettingsXml.Deserialize(str);
                        if (LoadSetting != null)
                        {
                            this.settings = LoadSetting;
                        }
                    }
                    catch
                    {
                        ErrorDialogs.PrintErrorDialog(CommonString.Error_in_settings_file);
                    }

                    str.Close();
                }
                catch
                {
                    ErrorDialogs.PrintErrorDialog(CommonString.Settings_file_not_found);
                }
            }
            else
                createdefault();
        }

        private void createdefault()
        {
            this.settings = new SettingsStruct(true);
            SaveSettings();
        }


        public void SaveSettings()
        {
            XmlSerializer SettingsXml = new XmlSerializer(typeof(SettingsStruct));
            try
            {
                using (Stream str = new FileStream(settings_file, FileMode.Create, FileAccess.ReadWrite, FileShare.Read))
                {
                    try
                    {
                        SettingsXml.Serialize(str, this.settings);
                    }
                    catch (Exception e) { }
                }
            }
            catch(Exception e)
            {
                ErrorDialogs.PrintErrorDialog(CommonString.Cant_write_in_settings_file);
            }
        }

        public static void ResourceActivator(string cultureStr, ResourceDictionary WindowDic)
        {
            ResourceDictionary dict = new ResourceDictionary();
            switch (cultureStr)
            {  
                case "en-US":
                    dict.Source = new Uri("..\\Resources\\StringResources.xaml", UriKind.Relative);
                    break;
                case "hy-AM":
                    dict.Source = new Uri("..\\Resources\\StringResources.hy-AM.xaml", UriKind.Relative);
                    break;
                default:
                    dict.Source = new Uri("..\\Resources\\StringResources.xaml", UriKind.Relative);
                    break;  
            }
            WindowDic.MergedDictionaries.Add(dict);
        }

        public static void MessageResourceActivator(string cultureStr)
        {
            CommonString.CultureStr = cultureStr;
        }

    }
}
